package com.snj.visangedu;

import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.Vibrator;
import android.util.Log;

import com.google.android.gcm.GCMBaseIntentService;

public class GCMIntentService extends GCMBaseIntentService {
	private final String PUSH_WRITTENREPLY = "1";
	
	private static final String tag = "GCMIntentService";
	public static final String SEND_ID = "834116972961";

	public GCMIntentService(){ this(SEND_ID); }
	
	public GCMIntentService(String senderId) { super(senderId); }

	@Override 
	protected void onMessage(Context context, Intent intent) {
		Bundle b = intent.getExtras();
		
		Log.d(tag, "Recv PUSH" + b.toString());
		
		AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
		NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
		
		ActivityManager activityManager = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> info;
		info = activityManager.getRunningTasks(1);
		RunningTaskInfo runningTaskInfo = info.get(0);
		String nowAPP = runningTaskInfo.topActivity.getClassName().toString();
		
		Log.d(tag, nowAPP);
		
		
		/*
		if(nowAPP.contains("com.snj.bestdealer") && ((PowerManager)context.getSystemService(Context.POWER_SERVICE)).isScreenOn())
		{
			
			Intent msgIntent = new Intent();
			msgIntent.setAction(getString(R.string.newrequest));
			msgIntent.putExtra("cmdtype", "issue");
			sendBroadcast(msgIntent);
            Log.d(tag, "Send Broadcast");
		}
		else
		{
			if(b.getString("type").equals("newrequest"))
    		{
    			NewRequest(context, b, audioManager.getRingerMode(), nm);
    		}
		}*/
	}
	
	private void NewRequest(Context c, Bundle b, int mode, NotificationManager nm)
	{	
		switch(mode){
		case AudioManager.RINGER_MODE_VIBRATE:

			Vibrator vibe1 = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
			vibe1.vibrate(new long[]{100, 300, 100, 300}, -1);
			//vibe1.vibrate(500);
			break;
		case AudioManager.RINGER_MODE_NORMAL:
			//MediaPlayer mp = MediaPlayer.create(c, R.raw.chime1);
			//mp.start();				

			break;
		case AudioManager.RINGER_MODE_SILENT:
	
			break;
		}	
		
		
	}
	
	@Override
	protected void onError(Context context, String errorId) {
		Log.d(tag, "onError. errorId : "+ errorId);
	}

	@Override
	protected void onRegistered(Context context, String regId) {
		Log.d(tag, "onRegistered. regId : "+regId);
		
		SharedPreferences pref = getSharedPreferences("Setting", 0);
		SharedPreferences.Editor edit = pref.edit();
		edit.putString("APP_DEVICE_KEY", regId);
		edit.commit();
	}

	@Override
	protected void onUnregistered(Context context, String regId) {
		Log.d(tag, "onUnregistered. regId : "+regId);
	}

	@Override
	protected boolean onRecoverableError(Context context, String errorId) {
		Log.d(tag, "onRecoverableError. errorId : "+errorId);
		return super.onRecoverableError(context, errorId);
	}
}